package com.spring.demo.redis.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @description:
 * @author: ZeckZheng
 * @time: 2023/10/25 23:53
 */
@Component
public class JedisUtil {

    @Autowired
    private JedisPool jedisPool;

    public String get(String key) {
        Jedis jedis = jedisPool.getResource();
        String value = jedis.get(key);
        jedis.close();
        return value;
    }

    public void set(String key, String value) {
        Jedis jedis = jedisPool.getResource();
        jedis.set(key, value);
        jedis.close();
    }

    public void setNx(String key, String value) {
        Jedis jedis = jedisPool.getResource();
        jedis.setnx(key, value);
        jedis.close();
    }

    public void setEx(String key, String value, int seconds) {
        Jedis jedis = jedisPool.getResource();
        jedis.setex(key, seconds, value);
        jedis.close();
    }

    public void del(String key) {
        Jedis jedis = jedisPool.getResource();
        jedis.del(key);
        jedis.close();
    }

    public void expire(String key, int seconds) {
        Jedis jedis = jedisPool.getResource();
        jedis.expire(key, seconds);
        jedis.close();
    }


}
